package com.hzit.servlet.ajax;

import com.hzit.dao.EmpDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "CheckEnameServletAjax", urlPatterns = "/emp/check/ename")
public class CheckEnameServletAjax extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //response.setContentType("application/json;charset=utf-8");

        String ename = request.getParameter("ename");

        //2.操作数据库
        EmpDao empDao = new EmpDao();

        //true:存在    false:不存在，可用
        boolean ok = empDao.isEnameExist(ename);

        //3.返回结果（不再是页面的跳转，直接把结果范围。客户端可以接收返回内容）
        response.getWriter().print(ok);

    }
}
